package com.pickgliss.light2d.texture
{
	import com.adobe.utils.AGALMiniAssembler;
	import com.pickgliss.ds.BitmapLib;
	import com.pickgliss.light2d.display.LightStage;
	import com.pickgliss.loaders.BitmapLoader;
	import com.pickgliss.ui.core.Disposeable;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DTextureFormat;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.Program3D;
	import flash.display3D.VertexBuffer3D;
	import flash.display3D.textures.Texture;
	import flash.geom.Matrix3D;
	import flash.utils.Dictionary;

	public class ImageTexture implements ITexture
	{
		private static var _imageIndexBuffer:IndexBuffer3D;

		private static var _imageProgram:Program3D;

		private static var _vertextBuffer:VertexBuffer3D;
		
		protected var _defaultMatrix:Matrix3D;

		public function ImageTexture()
		{
		}

		protected var _texture:Texture;
		public var textureHeight:uint=0;
		protected var _textureId:String="";
		public var textureWidth:uint=0;
		
		public function get textureId():String
		{
			return _textureId;
		}

		public function set textureId(value:String):void
		{
			_textureId = value;
		}

		public function get texture():Texture
		{
			return _texture;
		}

		public function set texture(value:Texture):void
		{
			_texture = value;
		}

		public function step():void
		{
			
		}

		public function dispose():void
		{
			texture=null;
		}

		public function getIndexBuffer():IndexBuffer3D
		{
			if (_imageIndexBuffer != null)
				return _imageIndexBuffer;
			_imageIndexBuffer=LightStage.context3D.createIndexBuffer(6);
			_imageIndexBuffer.uploadFromVector(Vector.<uint>([0, 1, 2, 0, 2, 3]), 0, 6);
			return _imageIndexBuffer;
		}

		public function getProgram():Program3D
		{
			if (_imageProgram != null)
				return _imageProgram;
			var vertexShaderAssembler:AGALMiniAssembler=new AGALMiniAssembler();
			vertexShaderAssembler.assemble(Context3DProgramType.VERTEX, "m44 op, va0, vc0\n" + // pos to clipspace
				"mov v0, va1" // copy uv
				);
			var fragmentShaderAssembler:AGALMiniAssembler=new AGALMiniAssembler();
			fragmentShaderAssembler.assemble(Context3DProgramType.FRAGMENT, "tex ft1, v0, fs0 <2d,linear,nomip>\n" + "mov oc, ft1");

			_imageProgram=LightStage.context3D.createProgram();
			_imageProgram.upload(vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode);
			return _imageProgram;
		}
		
		public function getDefaultMatrix():Matrix3D
		{
			if(_defaultMatrix == null)
			{
				_defaultMatrix = new Matrix3D();
				_defaultMatrix.appendScale(textureWidth / 2, textureHeight / 2, 1);
				_defaultMatrix.appendTranslation(textureWidth / 2,textureHeight / 2, 0);
			}
			return _defaultMatrix;
		}

		public function getVetextBuffer():VertexBuffer3D
		{
			if (_vertextBuffer != null)
				return _vertextBuffer;
			var vertextes:Vector.<Number>=Vector.<Number>([-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0]);
			_vertextBuffer=LightStage.context3D.createVertexBuffer(4, 3);
			_vertextBuffer.uploadFromVector(vertextes, 0, 4);
			return _vertextBuffer;
		}
		
		private static var _imageUVBuffer:VertexBuffer3D;
		public function getUVBuffer():VertexBuffer3D
		{
			if (_imageUVBuffer != null)
				return _imageUVBuffer;
			var vertextes:Vector.<Number>=Vector.<Number>([ 0, 0, 1, 0,  1, 1,  0, 1]);
			_imageUVBuffer=LightStage.context3D.createVertexBuffer(4, 2);
			_imageUVBuffer.uploadFromVector(vertextes, 0, 4);
			return _vertextBuffer;
		}
	}
}
